﻿
﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using XYTXL.AppCode;
using System.Data;
using XYTXL.Data;
using YanWF;

namespace XYTXL.View.Comon
{
    public partial class WFCommonAdd : BasePage
    {



        public WFFormUControl WFForm
        {

            get { return UserControl.Controls[0] as WFFormUControl; }
        }
        public JH_Auth_FormBase FormBaseData
        {
            get
            {
                return ViewState[hidFormCode.Value] == null ? new FormHelp().GetFormCodeByFormCode(hidFormCode.Value) : (JH_Auth_FormBase)ViewState[hidFormCode.Value];
            }
            set
            {
                ViewState[hidFormCode.Value] = value;
            }
        }

        protected override void OnInit(EventArgs e)
        {

            hidFormCode.Value = Request.QueryString["FormCode"] ?? "";
            //加载用户控件
            WFFormUControl Control = LoadControl("/View/View/UCMangForm/" + FormBaseData.FormAddUrl) as WFFormUControl;
            UserControl.Controls.Add(Control);
            base.OnInit(e);
            //为用户控件加载用户数据
            Control.UserInfo = UserInfo;
            lblTitle.Text = FormBaseData.FormDec;
            //是否具有暂存功能
            if (FormBaseData.FormISZC == "N")
            {
                btHoldForm.Visible = false;
            }

            if (FormBaseData.FormProcessDefinitionID == 0)
            {
                btSendForm.Text = "立即保存";
            }

            if (!IsPostBack)
            {
                Control.initALLForm(hidFormCode.Value);

                //初始化添加表单
                Control.initAddForm(hidFormCode.Value);
            }
        }

        protected void btHoldForm_Click(object sender, EventArgs e)
        {
            SaveForm("Y");
        }
        protected void btSendForm_Click(object sender, EventArgs e)
        {
            SaveForm();

        }
        /// <summary>
        /// 保存单据,如果是暂存，则冻结单据
        /// </summary>
        /// <param name="strIsSuspend"></param>
        public void SaveForm(string strIsSuspend = "N")
        {

            string strVerifyMsg = WFForm.VerifyAddForm(strIsSuspend);
            if (strVerifyMsg == "")
            {
                FormHelp FormHelp = new FormHelp();

                if (FormBaseData.FormProcessDefinitionID == 0)//普通表单
                {
                    //添加表单基本数据
                    int intFormID = FormHelp.addForm(FormBaseData.FormTable, UserInfo.User.UserName);
                    hidFormID.Value = intFormID.ToString();
                    //执行表单保存数据方法
                    try
                    {
                        WFForm.SaveForm(intFormID, "ADD");
                        AlertMSG("3", "操作成功");
                    }
                    catch (Exception)
                    {
                        FormHelp.DelForm(intFormID, FormBaseData.FormTable);
                    }
                }
                else//带流程表单
                {

                    WFForm.StartedProcessBefore();//执行表单流程开始前方法
                    string strTaskInstanceID = new WFHelp().StartProcess(int.Parse(FormBaseData.FormProcessDefinitionID.ToString()), UserInfo.User.UserName, FormBaseData.FormCode);
                    WFForm.StartedProcessEnd(strTaskInstanceID);//执行表单流程开始后方法
                    int intProcessStanceID = int.Parse(strTaskInstanceID.Substring(0, 6));
                    if (strTaskInstanceID != "0")
                    {
                        //如果暂存，则冻结当前流程
                        if (strIsSuspend == "Y")
                        {
                            new WFHelp().SuspendProcess(intProcessStanceID);
                        }
                        //添加表单基本数据
                        int intFormID = FormHelp.addForm(FormBaseData.FormTable, UserInfo.User.UserName, intProcessStanceID);
                        hidFormID.Value = intFormID.ToString();
                        try
                        {
                            //执行表单保存数据方法
                            WFForm.SaveForm(intFormID, "ADD", intProcessStanceID);
                            AlertMSG("3", "操作成功");
                        }
                        catch (Exception)
                        {
                            //保存数据失败,删除表单数据及流程数据
                            FormHelp.DelForm(intFormID, FormBaseData.FormTable);
                            new WFHelp().delProcessTask(intProcessStanceID);
                        }
                        //保存通知数据
                        FormHelp.SaveFormConfirms(intProcessStanceID.ToString(), hidConfirmUsers.Value, FormBaseData.FormCode,intFormID, FormBaseData.FormDec, UserInfo.User.UserName);
                        FormHelp.SendFormConfirm(intProcessStanceID.ToString());
                        //发送通知
                        //
                        hidProcessID.Value = intProcessStanceID.ToString();

                    }
                }
                btSendForm.Enabled = false;
                btHoldForm.Enabled = false;
            }
            else
            {
                AlertMSG("1", strVerifyMsg);
            }
        }




    }
}